<?php

namespace App\Services;

use App\Models\ModuleConfig;
use App\Models\Site;

class ConfigService
{
    public function saveCurrentModuleConfig(array $config)
    {
        //将配置项存入库中
        $model = ModuleConfig::firstOrNew(['site_id' => site()['id'], 'module_id' => module()['id']]);
        //使用+防止多个配置项覆盖。
        $model->config = array_merge($model->config ?? [], $config);

        $model->save();
    }

    public function loadSiteConfig()
    {
        config(['site' => site()['config']]);
    }

    public function saveSiteConfig(Site $site = null)
    {
        site($site);
    }

    //读取库中配置项，存入config类
    public function loadCurrentModuleConfig()
    {
        $module = ModuleConfig::where('site_id', site()['id'])->where('module_id', module()['id'])->first();
        if ($module) {
            config(['module' => $module['config']]);
            return $module['config'];
        }
    }

}
